/*
 * @(#)SetCharacterEncodingFilter.java 1.0 2005-3-2
 * Copyright 2005 VandaGroup, Inc. All rights reserved.
 */
package com.vandagroup.portal;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;

public class SetCharacterEncodingFilter implements Filter {
	protected FilterConfig config = null;

	protected String encoding = null;

	protected boolean ignore = true;

	public SetCharacterEncodingFilter() {
		super();
	}

	public void destroy() {
		this.encoding = null;
		this.config = null;
	}

	public void doFilter(ServletRequest request, ServletResponse response,
			FilterChain chain) throws ServletException, IOException {
		if (this.ignore || request.getCharacterEncoding() == null) {
			request.setCharacterEncoding(this.encoding);
		}
		chain.doFilter(request, response);
	}

	public void init(FilterConfig config) {
		this.config = config;
		this.encoding = this.config.getInitParameter("encoding");
		String value = this.config.getInitParameter("ignore");
		if (value == null) {
			this.ignore = true;
		} else if (value.equalsIgnoreCase("true")) {
			this.ignore = true;
		} else {
			this.ignore = false;
		}
	}
}
